package com.offcn.service.Impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.offcn.mapper.OrdersettingMapper;
import com.offcn.pojo.Ordersetting;
import com.offcn.service.OrdersettingService;
import org.springframework.beans.factory.annotation.Autowired;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Author: wy
 * @Date: 2022/9/1 10:51
 */
@Service
@org.springframework.stereotype.Service
public class OrdersettingServiceImpl implements OrdersettingService {
    @Autowired
    private OrdersettingMapper ordersettingMapper;


    //exal上传
    @Override
    public void uploadTempleate(List<String[]> ordersettings) {
        LocalDate now = LocalDate.now();

        //我们对传递过来的 上传文件进行遍历
        for (String[] strings : ordersettings) {
            //我们对于获取到的 数据 要进行判断 他表格里面的 时间 是否 > || < 当前时间

            String olddateExcel = strings[0];//获取表格中的时间 因为格式不对 我们要对其进行 格式化
            DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy/MM/dd");
            LocalDate localDate = LocalDate.parse(olddateExcel, dateTimeFormatter);

            //随后我们 让这个时间 和 现在时间进行对比
            if (localDate.isAfter(now) || localDate.isEqual(now)) {
                //我们在对其进行判断 他这个时间 在数据库中是否存在
                LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                lambdaQueryWrapper.eq(Ordersetting::getOrderdate, localDate);
                Ordersetting ordersetting1 = ordersettingMapper.selectOne(lambdaQueryWrapper);
                if (ordersetting1 != null) {
                    //进行修改
                    //ordersetting1不为空说明该时间已经存在，修改可预约数量
                    ordersetting1.setNumber(Integer.parseInt(strings[1]));
                    ordersettingMapper.updateById(ordersetting1);

                } else {
                    //进行添加
                    Ordersetting ordersetting = new Ordersetting();
                    ordersetting.setOrderdate(localDate);
                    ordersetting.setNumber(Integer.parseInt(strings[1]));
                    ordersettingMapper.insert(ordersetting);
                }
            }
        }
    }

    @Override
    public List<Map<String, Object>> getOrdersettingByDate(String date) {
        List<Map<String, Object>> mapList = new ArrayList<>();
        //设置 一个月的 最初值 和最后 一天的 值
        String startDate = date + "-" + 1;
        String endDate = date + "-" + 30;

        LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.between(Ordersetting::getOrderdate, startDate, endDate);


        List<Ordersetting> ordersettings = ordersettingMapper.selectList(lambdaQueryWrapper);

        for (Ordersetting ordersetting : ordersettings) {
            Map<String, Object> map = new HashMap<>();
            //这里的key必须结合页面中需要的json数据的key一致
            map.put("date", ordersetting.getOrderdate().getDayOfMonth());
            map.put("number", ordersetting.getNumber());
            map.put("reservations", ordersetting.getReservations());
            mapList.add(map);
        }
        return mapList;
    }

    @Override
    public void editNumberByDate(Ordersetting ordersetting) {
        //先判断 这个日期 在数据库里面是否存在
        LambdaQueryWrapper<Ordersetting> lambdaQueryWrapper = new LambdaQueryWrapper<>();

        lambdaQueryWrapper.eq(Ordersetting::getOrderdate, ordersetting.getOrderdate());

        Ordersetting ordersetting1 = ordersettingMapper.selectOne(lambdaQueryWrapper);
        if (ordersetting1 != null) {
            //进行修改 ———— 根据时间
            ordersettingMapper.update(ordersetting, lambdaQueryWrapper);
        } else {
            //直接添加
            ordersettingMapper.insert(ordersetting);
        }
    }
}
